﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Bowling_Recoder
{
    public class Game
    {


        private int ball;
        private int firstThrow;
        private int secondThrow;

        private int itsScore = 0;
        private int[] itsThrows = new int[21];//存储每次投掷的瓶数
        private int itsCurrentThrow = 0;
        private int itsCurrentFrame = 1;
        private bool firstThrowInFrame = true;



        public int Score()//总分
        {
            return ScoreForFrame(GetCurrentFrame()-1);
        }
        public int GetCurrentFrame()//投掷的轮数
        {
            return itsCurrentFrame;
        }


        public void Add(int pins)//每轮撞倒球数量
        {
            itsThrows[itsCurrentThrow++] = pins;
            itsScore += pins;
            AdjustCurrentFtame(pins);
        }
        private void AdjustCurrentFtame(int pins)
        {
            if (firstThrowInFrame==true )
            {
                if(pins==10)
                {
                    itsCurrentFrame++;
                }
                else
                {
                    firstThrowInFrame = false ;            
                }
            }
            else
            {
                firstThrowInFrame = true;
                itsCurrentFrame++;
            }
           
            itsCurrentFrame = Math.Min(11, itsCurrentFrame);
        }
        

       // public int ScoreForFrame(int frame)//每轮的分数
       //// {
        //    int score = 0;
        //    for(int ball=0;frame >0&&(ball<itsCurrentThrow);ball+=2,frame--)
        //    {
        //        score += itsThrows[ball] + itsThrows[ball + 1];
        //    }
        //    return score;
        //}

        public int ScoreForFrame(int theFrame)
        {
            ball = 0;
            int score = 0;
            for (int currentFrame=0;currentFrame <theFrame;currentFrame ++)
            {
                firstThrow = itsThrows[ball++];
                if (Strike())
                {
                    score += 10 + NextTwoBalls();

                }
                else if (Spare()) 
                {
                    score += 10 + NextBall();

                }
                else
                {
                    score += TwoBallInFrame();
                }
            }
            return score;
           
        }

        private int TwoBallInFrame()
        {
            return itsThrows[ball] + itsThrows[ball + 1];
        }

        public int HandleSecondThrow()
        {
            int score = 0;
            secondThrow = itsThrows[ball++];
            int frameScore = firstThrow + secondThrow;
            if(frameScore ==10)
            {
                score += frameScore + itsThrows[ball];
            }
            else
            {
                score += frameScore;
               
            }
            return score;
        }
        private bool Strike()
        {
            return itsThrows[ball] == 10;
        }
        private int NextTwoBalls()
        {
            return itsThrows[ball] + itsThrows[ball + 1];
        }
        private bool Spare()
        {
            return (itsThrows[ball] + itsThrows[ball +1]) == 10;
               
        }
        private int NextBall()
        {
            return itsThrows[ball];
        }
    }
}
